public class House2 implements Runnable {
    int waterAmount;
    Thread dog, cat;

    House2() {
        dog = new Thread(this);
        cat = new Thread(this);
    }

    public void setWater(int w) {

        waterAmount = w;
    }

    public void run() {
        while (true) {
            Thread t = Thread.currentThread();
            if (t == dog) {
                System.out.println("家狗喝水ˮ");
                waterAmount = waterAmount - 2;
            } else if (t == cat) {
                System.out.println("家猫喝水ˮ");
                waterAmount = waterAmount - 1;
            }
            System.out.println(" 剩余" + waterAmount);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            if (waterAmount <= 0) {
                return;
            }
        }
    }
}
